package org.cuijq.thinking.in.spring.ioc.depencdeycy.logkup;

import org.cuijq.thinking.in.spring.annotation.Super;
import org.cuijq.thinking.in.spring.ioc.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;

public class DependencyLouDemo {
    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-lookuo-context.xml");
        lookupByType(beanFactory);
        
        lookupByCollectType(beanFactory);
        lookupByAnnotation(beanFactory);
//        lookupRealTime(beanFactory);
//        lookupInLazy(beanFactory);
    }

    private static void lookupByAnnotation(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory){
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String ,User> users=(Map)listableBeanFactory.getBeansWithAnnotation(Super.class);
            System.out.println("查找标注@Super 所有的集合 对象"+users);
        }
    }

    private static void lookupByCollectType(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory){
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String ,User> users=listableBeanFactory.getBeansOfType(User.class);
            System.out.println("查找所有的User 对象"+users);
        }
    }

    private static void lookupByType(BeanFactory beanFactory) {
        User user= beanFactory.getBean(User.class);
        System.out.println("泛型"+user);
    }

    private static void lookupInLazy(BeanFactory beanFactory) {
        ObjectFactory<User> objectFactory = (ObjectFactory<User>)beanFactory.getBean("objectFactory");
        User user = objectFactory.getObject();
        System.out.println("延迟查找"+user);
    }

    public static void lookupRealTime(BeanFactory beanFactory){
        User user = (User)beanFactory.getBean("user");
        System.out.println("实时查找"+user);
    }
}
